/**
 * @author sce
 * @description 增加restFul风格的方法
 */

import { BaseContext } from 'koa';

export default (routerPrefix: string) => {
    return async (ctx: BaseContext, next: any) => {
        if (ctx.request.path.startsWith(routerPrefix)) {
            ctx.rest = (data: object|any) => {
                ctx.response.type = 'application/json';
                ctx.response.body = {
                    code: 0,
                    message: 'ok',
                    data
                };
            };
        }
        await next();
    };
};